<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/9
 * Time: 16:49
 */
require_once "NiuNiuCard.php";
require_once "NiuniuType.php";
require_once "AdminManager.php";
class NiuNiuManager
{
    private $niuNiu=array();//当前牌堆
    //private $curCardNum=0;//当前第几张牌
   // private $totalNum=52;//总牌数

    private $startIndex=0;//牌堆开始的索引


    function __construct()
    {
        $this->initPai();
        $this->xipai();
        $this->startIndex = mt_rand(0, 51);

    }

    function getNiuPile(){
        return $this->niuNiu;
    }
    function initPai(){
        //生成牌型
        for($type = 1; $type <= 4; $type++){
            for($num = 1; $num <= 13; $num++ ){
                array_push($this->niuNiu, new NiuNiuCard($type, $num, $num > 10 ? 10 : $num));
               // $this->curCardNum++;
            }
        }
    }

    function xiPai(){
        //洗牌
        shuffle($this->niuNiu);
        shuffle($this->niuNiu);
        shuffle($this->niuNiu);
    }

    /**
     * 发牌函数，说明，为了适应原始版本，如果有管理员的情况下，此函数先给管理员发牌，再普通发牌
     * @param NiuniuType admin: 管理员要求的牌型，默认为-1(不是管理员)，传数据格式为NiuniuType.php中定义的常量
     * @return array //返回发牌的结果
     */
    function faPai($adminPaiXing = -1){
        if($adminPaiXing != -1){
            return $this->adminFaPai($adminPaiXing);
        }
        else{
            return $this->normalfaPai();
        }
    }

    /**
     * 普通发牌
     * @return array //返回发牌的结果
     */
    private function normalfaPai(){
        //发牌
        $quPai=array();
        for($i=0;$i<5;$i++){
            $quPai[$i]=$this->niuNiu[$this->startIndex];
            $this->startIndex++;//让当前索引自加1
            //取模以保证循环,由于有管理员，管理员取牌后，需要删除取掉的牌，剩余的牌会减少
            $this->startIndex %= count($this->niuNiu);
        }
        return $quPai;
    }

    /**
     * @param $adminPaiXin 管理员牌型
     * @return array //返回发牌的结果
     */
    private function adminFaPai($adminPaiXin){
        //如果是普通牛牌型，从牛1-牛牛
        if($adminPaiXin >= NIUNIU1 && $adminPaiXin <= NIUNIU){
            $adminPaiXin -= 80;
            return AdminManager::createPuTongNiu($this->niuNiu, $adminPaiXin);
        }
        //否则是特殊牌型，都必须单独创建
        else{
            switch ($adminPaiXin){
                case TONGHUASHUN:
                    return AdminManager::createTongHuaShunNiu($this->niuNiu);
                case TONGHUANIU:
                    return AdminManager::createTongHuaNiu($this->niuNiu);
                case WUHUANIU:
                    return AdminManager::createWuHuaNiu($this->niuNiu);
                case SIHUANIU:
                    return AdminManager::createSiHuaNiu($this->niuNiu);
                case WUXIAONIU:
                    return AdminManager::createWuXiaoNiu($this->niuNiu);
                case SHUNZINIU:
                    return AdminManager::createShunZiNiu($this->niuNiu);
                case ZHADANNIU:
                    return AdminManager::createZhaDanNiu($this->niuNiu);
                case HULUNIU:
                    return AdminManager::createHuLuNiu($this->niuNiu);

            }
        }



    }


}